<%@Page Language="VB" %>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Updating Data with a Transacted Stored Procedure</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Updating Data with a Transacted Stored Procedure</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>Stored Procedure Name: <b><span id="outSQL" runat="server"></span></b></div>
<div id="outError" runat="server">&nbsp;</div>
<div>Input Parameter: <b><span id="outInParams" runat="server"></span></b></div>
<div>Output Parameter: <b><span id="outOutParams" runat="server"></span></b></div>

<script language="vb" runat="server">

Sub Page_Load()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect = ctlConnectStrings.OLEDBConnectionString
   outConnect.InnerText = strConnect 'and display it

   'specify the stored procedure name
   Dim strSQL As String = "DoBookArchive"
   outSQL.InnerText = strSQL   'and display it

   'create a new Connection object using the connection string
   Dim objConnect As New OleDbConnection(strConnect)

   'create a new Command using the connection object and stored proc name
   Dim objCommand As New OleDbCommand(strSQL, objConnect)
   objCommand.CommandType = CommandType.StoredProcedure

   'create a variable to hold a Parameter object
   Dim objParam As OleDbParameter

   'create a new Parameter object named 'ISBN' with the correct data
   'type to match a SQL database 'varchar' field of 12 characters
   objParam = objCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12)

   'specify that it's an Input parameter and set the value
   objParam.Direction = ParameterDirection.Input
   objParam.Value = "1999999999"

   'create an output Parameter object named 'Result' with the correct
   'data type to match a SQL database 'integer' field
   'specify that it's an Output parameter so no value required
   objParam = objCommand.Parameters.Add("Result", OleDbType.Integer)
   objParam.Direction = ParameterDirection.Output

   'display the value of the input parameter
   outInParams.InnerText = "ISBN='" & objCommand.Parameters("ISBN").Value & "'"

   Try

      'open the connection to the database
      objConnect.Open()

      'execute the SQL statement against the command
      objCommand.ExecuteNonQuery()
      objConnect.Close()

   Catch objError As Exception

      'display error details
      outError.InnerHtml = "<b>* Error while updating original data</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'collect and display the value of the output parameter
   Dim intResult As Integer = objCommand.Parameters("Result").Value
   Dim strResult As String = "Result='" & CStr(intResult) & "'<br />"

   Select Case intResult
     Case -1: strResult += "Error occurred while attempting archive"
     Case 0: strResult += "Failed to archive book - no matching book found"
     Case > 0: strResult += "Successfully archived the specified book"
   End Select

   outOutParams.InnerHtml = strResult

End Sub
</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
